import { http } from "@/utils/http";
import { ElMessage } from "element-plus";

enum Index {
  page = "/page",
  list = "/list",
  batch = "/batch",
  batchDel = "/batch/del",
  upload = "/upload",
  download = "/download"
}

export type R<T = object> = {
  code: number;
  msg: string;
  data: T;
  timestamp: number;
  success: boolean;
};

/**
 * 导出文件xlsx的mime-type
 */
const XLSX_MIME_TYPE =
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

/**
 * 导出文件xlsx的文件后缀
 */
const XLSX_FILE_SUFFIX = ".xlsx";

/**
 * create
 * @param path
 * @param data
 */
export const create = (path: string, data?: object) =>
  http.post(path, { data: data });

/**
 * batch create
 * @param path
 * @param data
 */
export const batchCreate = (path: string, data?: object) =>
  http.post<R, {}>(`${path}${Index.batch}`, { data: data });

/**
 * page
 * @param path
 * @param data
 */
export const page = (path: string, data?: object) =>
  http.post<R, {}>(`${path}${Index.page}`, { data: data });

/**
 * list
 * @param path
 * @param params
 * @param data
 */
export const list = (path: string, data?: object) =>
  http.post<R, {}>(`${path}${Index.list}`, { data: data });

/**
 * detail
 * @param path
 * @param id
 */
export const detail = (path: string, id?: string) =>
  http.get<R, {}>(`${path}/${id}`);

/**
 * count
 * @param path
 * @param pathVariable
 */
export const count = (path: string, pathVariable?: string) =>
  http.get<R, {}>(`${path}${pathVariable}`);

/**
 * edit
 * @param path
 * @param data
 */
export const edit = (path: string, data?: object) =>
  http.put<R, {}>(path, { data: data });

/**
 * del
 * @param path
 * @param id
 */
export const del = (path: string, id?: string) => {
  return http.delete<R, {}>(`${path}/${id}`);
};

/**
 * batch delete
 * @param path
 * @param data
 */
export const batchDel = (path: string, data?: object) => {
  return http.delete<R, {}>(`${path}${Index.batchDel}`, { data: data });
};

/**
 * download excel
 * @param path
 * @param params
 * @param data
 * @param isXlsx
 */
export const download = (
  path?: string,
  params?: object,
  data?: any,
  isXlsx = false
) => {
  http
    .post(`${path}${Index.download}`, {
      params: params,
      data: data,
      responseType: "blob"
    })
    .then(result => {
      if (!result) {
        ElMessage.warning("文件下载失败");
        return;
      }
      let name;
      if (!data.excel.name) {
        name = "导出文件";
      } else {
        name = data.excel.name;
      }
      const blobOptions = { type: XLSX_MIME_TYPE };
      let fileSuffix = ".xls";
      if (isXlsx) {
        blobOptions["type"] = XLSX_MIME_TYPE;
        fileSuffix = XLSX_FILE_SUFFIX;
      }
      const nav = window.navigator as any;
      if (nav.msSaveBlob) {
        nav.msSaveBlob(
          new Blob([result as BlobPart], blobOptions),
          name + fileSuffix
        );
      } else {
        const url = window.URL.createObjectURL(
          new Blob([result as BlobPart], blobOptions)
        );
        const link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", name + fileSuffix);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        window.URL.revokeObjectURL(url);
      }
    })
    .finally(() => {});
};

/**
 * upload excel
 * @param path
 * @param data
 */
export const upload = (path: string, data?: any) => {
  const formData = new FormData();
  formData.append("file", data.file);
  return http.post<R, {}>(`${path}${Index.upload}`, {
    headers: {
      "Content-type": "multipart/form-components"
    },
    data: formData
  });
};
